<template>
  <view class="index-container">
    <CanvasPage :scroll-top="scrollTop" />
    <!-- <ADWindow /> -->
    <ReturnTop :scroll-top="scrollTop" />
  </view>
</template>

<script setup>
import { onLoad, onPageScroll, onShareAppMessage, onShareTimeline,onShow } from '@dcloudio/uni-app'
import { useMainStore } from '@/store/modules/useMainStore'
import CanvasPage from '@/components/canvasShow/canvasShowPage.vue'
import ReturnTop from "@/components/ReturnTop/index.vue"
import ADWindow from "@/components/adWindow/adWindow.vue"
import { useScroll } from "@/hooks/useScroll";
import { useShare } from "@/hooks/useShare";
import { parseQuery ,h5InWeChat,afterLogin} from "@/utils";
import { wechatAuth } from "@/api/auth";
import cookie from "@/utils/cookie";

const main = useMainStore()

const {shareAppMessage, shareTimeline} = useShare();
onShareAppMessage(shareAppMessage)
onShareTimeline(shareTimeline)
const {scrollTop} = useScroll()

onPageScroll(() => {
})

onLoad(() => {
  main.init()
})

onShow(() => {
  const { code } = parseQuery()
	 if(h5InWeChat() && code){
		auth(code)
	 } 
})

const auth = async(code) => {
	console.log('获取微信授权:',code)
	var token = cookie.get('accessToken')
	if (!token) {	
		let data = await wechatAuth({'code':code,'invitationCode':''})
		if (data) {
			await main.setAccessToken(data)
			await main.getUserInfo()
			afterLogin()
		}
	}
}


</script>

<style lang="scss">
.index-container {
  width: 100%;
  position: relative;
}
</style>
